﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;
using Telerik.Web.UI;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected override void OnInit(EventArgs e)
    {
        btnSearch.Click += new EventHandler(btnSearch_Click);
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {   
            if (AcmsPage != null && AcmsPage.AcmsContext != null && AcmsPage.AcmsContext.CurrentPost != null)
            {
                var pathParts = AcmsPage.AcmsContext.CurrentPost.Path.Split('/');
                if (pathParts.Length > 1)
                    form1.Attributes["class"] = "page-" + pathParts[pathParts.Length - 1];
                else
                    form1.Attributes["class"] = "home";
            }
        }
    }

    private bool DataBound = false;

    protected override void OnPreRender(EventArgs e)
    {
        if (!IsPostBack && !DataBound)
        {
            DataBind();
        }
        base.OnPreRender(e);
    }

    protected Acms.AcmsControls.TemplatePage AcmsPage
    {
        get
        {
            var tmp = this.Page as Acms.AcmsControls.TemplatePage;
            return tmp;
        }
    }

    protected void dsBreadcrumbs_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        var acmsPage = this.Page as Acms.AcmsControls.TemplatePage;
        if (acmsPage != null && acmsPage.AcmsContext.CurrentPost != null)
        {
            var posts = new List<Post>();
            posts.Add(acmsPage.AcmsContext.CurrentPost);
            while (posts.Last().Parent != null)
            {
                posts.Add(posts.Last().Parent);
            }
            posts.Reverse();
            e.SetData(posts);
        }
        else if (acmsPage != null)
        {
            //e.SetData(Acms.Bll.DummyPost.CreateDummyList(5));
            e.SetData(Acms.Bll.Post.GetTopLevelBySiteId(acmsPage.AcmsContext.SiteId, acmsPage.AcmsContext.SiteVersion));
        }
    }

    void btnSearch_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (AcmsPage != null)
            {
                System.Text.StringBuilder url = new System.Text.StringBuilder();
                url.Append(Page.ResolveUrl(AcmsPage.AcmsContext.GetUrl("~/Contact/Search.aspx")));
                if (url.ToString().Contains("?"))
                {
                    url.Append("&");
                }
                else
                {
                    url.Append("?");
                }
                url.AppendFormat("Term={0}", Server.UrlEncode(txtSearch.Text));
                Response.Redirect(url.ToString(), true);
                /*Post search = Post.GetBySiteIdAndPath(page.AcmsContext.SiteId, "~/Search", page.AcmsContext.SiteVersion);
                if(search != null)
                {
                    page.AcmsContext = new AcmsContext(search, "Default");
                    System.Text.StringBuilder url = new System.Text.StringBuilder( page.AcmsContext.TemplateUrl);
                    if (url.ToString().Contains("?"))
                    {
                        url.Append("&");
                    }
                    else
                    {
                        url.Append("?");
                    }
                    url.AppendFormat("Term={0}", Server.UrlEncode(txtSearch.Text));
                    Server.Transfer(url.ToString(), false);
                }*/
            }
        }
    }

    public override void DataBind()
    {
        if (!DataBound)
        {
            //get the posts
            RadMenu1.DataSource = GetMenuDataSource();
            base.DataBind();
            DataBound = true;
        }

    }

    private AcmsMenuItemEnumerator GetMenuDataSource()
    {
        var posts = Acms.Bll.Post.GetAllByMenuId(this.AcmsPage.AcmsContext.SiteId, 1, this.AcmsPage.AcmsContext.SiteVersion);

        var roots = new AcmsMenuItemEnumerator(null);
        roots.Items.Add(RecursiveBuild(posts.First(), posts));
        //dont include parent
        return roots.Items.First().Children;
    }

    private AcmsMenuItem RecursiveBuild(Acms.Bll.Post current, List<Post> allPosts)
    {
        var tmp = new AcmsMenuItem(current.Id, current.DisplayName, current.Url, current.Path);
        var children = allPosts.Where(x => x.ParentPath == tmp.Path + "/");
        foreach (var child in children)
        {
            tmp.Children.Items.Add(RecursiveBuild(child, allPosts));
        }
        return tmp;
    }

    /*private void AddMenuItems(AcmsMenuItemEnumerator nodes, IEnumerator<Acms.Bll.Post> posts)
    {
        var last = (Acms.Bll.Post)null;
        var current = (AcmsMenuItem)null;
        AcmsMenuItemEnumerator currentCollection = nodes;
        var currentParent = (AcmsMenuItem)null;
        while (posts.MoveNext())
        {
            var tmp = new AcmsMenuItem(posts.Current.Id, posts.Current.DisplayName, posts.Current.Url, posts.Current.Path);
            if (last != null)
            {
                if (tmp.Path == last.Path)
                {
                    //same level
                }
                else if(tmp.Path
                {
                }
            }
            currentCollection.Items.Add(current);
            last = posts.Current;
        }
    }

    private void AddChild(AcmsMenuItem parent)
    {
    }*/
}
